/*********************************************************************************
 *      Copyright:  (C) 2022 South-Central Minzu University
 *                  All rights reserved.
 *
 *       Filename:  network.c
 *    Description:  This file :Client initialization, establish connection and check connection
 *                 
 *        Version:  1.0.0(12/04/22)
 *         Author:  zhangkang <2980640573@qq.com>
 *      ChangeLog:  1, Release initial version on "12/04/22 18:19:25"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>          
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <getopt.h>
#include <signal.h>
#include <libgen.h>
#include <linux/tcp.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netdb.h>


#include "logger.h"



int client_socket_init (char* servip,int port,int * sockfd) 
{
   
    const char              *ptr=NULL;
    char                   **pptr=NULL;
    char                     buf_dns[256];
    struct hostent          *getname=NULL;
    struct sockaddr_in 	     servaddr;
    struct in_addr           inaddr;
    int                      conret;
    
    if(  !(inet_aton(servip,&inaddr))  )
    {     
        getname = gethostbyname(servip);
        if(getname == NULL)
        {
            log_error("Get host_alias failure:%s\n",strerror(errno));
            return -1;
        }
        memset(buf_dns,0,sizeof(buf_dns));
        pptr = getname->h_addr_list;
        if(NULL == (ptr = inet_ntop(getname->h_addrtype,*pptr,buf_dns,sizeof(buf_dns))))
        {
            log_error("failure:%s\n",strerror(errno));
            return -2;
        }
        memcpy(&servip,&ptr,sizeof(ptr));  
    }
    
    log_info("servip:%s\n",servip);
    
    (*sockfd) = socket(AF_INET,SOCK_STREAM,0);
       
    if(  (*sockfd) < 0 )
    {
        log_error("Create socket failure:%s\n",strerror(errno));
        return -3;
    }
    
    log_info("SOCKET[%d]\n",(*sockfd));
    
    memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_port=htons(port);
    inet_aton(servip,&servaddr.sin_addr);
    
    conret=connect( (*sockfd),(const struct sockaddr *)&servaddr,sizeof(servaddr));
    
    if(  conret<0  )
    {
         log_warn(" connect server failure :%s\n",strerror(errno));
         return -4;
    }
    else
    {
        log_info("connect to server scuessful\n");
        return (*sockfd);
    }
    
}


int socket_connect_check(int sockfd)    
{
    
    struct tcp_info          info;
    
    if (sockfd <= 0)
    {
        return 0;
    }
    int len = sizeof(info);

    getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t *) & len);
    if ((info.tcpi_state == 1)) 
    {
        log_info("socket connected\n");
        return 1;
    } 
    else
    {
        log_error("socket disconnected\n");
        return 0;
    }
}  